package demo5;
//深拷贝
//clone方法的应用
//拷贝对象

//equals判断对象是否相等

import java.util.Objects;



class Money implements Cloneable{
    public double money = 12.25;

    //重写Object父类的clone方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Money money1 = (Money) o;
        return Double.compare(money1.money, money) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(money);
    }
}

class Student implements Cloneable{
    public String name;
    public Money m = new Money();

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    //这个方法不能深拷贝Student里的Money对象
    /*@Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }*/

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // 只是克隆了Student对象
        Student student = (Student)super.clone();

        // 克隆了 Student对象 里面的Money对象
        student.m = (Money)this.m.clone();

        return student;
    }

    /*public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        //指向的是同一个对象
        if(this == obj) {
            return true;
        }
        if(! (obj instanceof Student)) {
            return false;
        }
        Student student = (Student)obj;
        if(this.name.equals(student.name)) {
            return true;
        }
        return false;
    }*/

//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Student student = (Student) o;
//        return Objects.equals(name, student.name);
//    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name) && Objects.equals(m, student.m);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}


public class TestDemo5 {
    public static void main(String[] args) {
        Student student1 = new Student();
        student1.name = "zhangsan";

        Student student2 = new Student();
        student2.name = "zhangsan";

        //我们希望这两个对象 放到同一个位置去
        System.out.println(student1.hashCode());
        System.out.println(student2.hashCode());
    }

    //比较对象
    public static void main3(String[] args) {
        Student student1 = new Student();
        student1.name = "zhangsan";
        Student student2 = new Student();
        student2.name = "zhangsan";

        boolean flg = student1.equals(student2);
        System.out.println(flg);
    }


    //拷贝对象
    public static void main2(String[] args) throws CloneNotSupportedException  {
        Student student1 = new Student();
        Student student2 = (Student)student1.clone();
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
        System.out.println("================");
        student2.m.money = 99;
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
    }

    public static void main1(String[] args) throws CloneNotSupportedException  {
        Student student1 = new Student();
        student1.name = "hahaha";
        Student student2 = (Student)student1.clone();
        System.out.println(student1);
        System.out.println(student2);
    }
}
